//===============================================================================
// Microsoft patterns & practices
// Smart Client Software Factory
//===============================================================================
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by the "Add View" recipe.
//
// This class is the concrete implementation of a View in the Model-View-Presenter 
// pattern. Communication between the Presenter and this class is acheived through 
// an interface to facilitate separation and testability.
// Note that the Presenter generated by the same recipe, will automatically be created
// by CAB through [CreateNew] and bidirectional references will be added.
//
// For more information see:
// ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.scsf.2006jun/SCSF/html/03-030-Model%20View%20Presenter%20%20MVP%20.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using System;
using System.Windows.Forms;
using GlobalBank.Infrastructure.UI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

namespace GlobalBank.BranchSystems.Module.Views
{
	[SmartPart]
	public partial class FindCustomerView : UserControl, IFindCustomerView, ISmartPartInfoProvider
	{
		bool _isModalWindow = true;

		public FindCustomerView()
		{
			InitializeComponent();
		}

		public bool IsModalWindow
		{
			get { return _isModalWindow; }
			set { _isModalWindow = value; }
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			ReflectValidStatus();

			foreach (Control ctrl in Controls)
			{
				TextBox tb = ctrl as TextBox;
				if (tb != null)
				{
					tb.TextChanged += new EventHandler(TextBox_TextChanged);
				}
			}
		}

		void TextBox_TextChanged(object sender, EventArgs e)
		{
			ReflectValidStatus();
		}

		/// <summary>
		/// Sets the presenter. The dependency injection system will automatically
		/// create a new presenter for you.
		/// </summary>
		[CreateNew]
		public FindCustomerViewPresenter Presenter
		{
			set
			{
				_presenter = value;
				_presenter.View = this;
			}
		}

		public ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)
		{
			ISmartPartInfo spi;


			if (smartPartInfoType.IsAssignableFrom(typeof (WindowSmartPartInfo)))
			{
				WindowSmartPartInfo wspi = new WindowSmartPartInfo();
				wspi.Modal = this._isModalWindow;
				wspi.MinimizeBox = false;
				wspi.MaximizeBox = false;
				wspi.ControlBox = true;

				wspi.Keys[WindowWorkspaceSetting.FormBorderStyle] = FormBorderStyle.FixedDialog;
				wspi.Keys[WindowWorkspaceSetting.AcceptButton] = _findButton;
				wspi.Keys[WindowWorkspaceSetting.CancelButton] = _cancelButton;
				spi = wspi;
			}
			else
			{
				spi = Activator.CreateInstance(smartPartInfoType) as ISmartPartInfo;
			}

			spi.Description = Properties.Resources.FindCustomerViewDescription;
			spi.Title = Properties.Resources.FindCustomerViewTitle;

			return spi;
		}

		private void _cancelButton_Click(object sender, EventArgs e)
		{
			_presenter.OnCancel();
		}

		private void _findButton_Click(object sender, EventArgs e)
		{
			_errorLabel.Visible = false;
			FindCustomer();
		}

		private void FindCustomer()
		{
			_presenter.FindCustomer(
				_firstNameTextBox.Text, _lastNameTextBox.Text, _middleInitialTextBox.Text,
				_ssnTextBox.Text, _streetTextBox.Text, _cityTextBox.Text, _stateTextBox.Text,
				_zipTextBox.Text, _homeNumberTextBox.Text, _workNumberTextBox.Text, _cellNumberTextBox.Text,
				_emailTextBox.Text);
		}

		private void ReflectValidStatus()
		{
			// Update the Enabled Status for the find button
			bool enabled = false;
			foreach (Control ctrl in Controls)
			{
				TextBox tb = ctrl as TextBox;
				if (tb != null)
				{
					enabled = enabled || tb.Text.Length > 0;
					if (enabled) break;
				}
			}
			_findButton.Enabled = enabled;
		}

		public void ShowMessage(string message)
		{
			_errorLabel.Text = message;
			_errorLabel.Visible = true;
		}

		private void _firstNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidAlphaChar(e.KeyChar);
		}

		private void _middleInitialTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidAlphaChar(e.KeyChar);
		}

		private void _lastNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidAlphaChar(e.KeyChar);
		}

		private void _cityTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidAlphaChar(e.KeyChar);
		}

		private void _stateTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidAlphaChar(e.KeyChar);
		}

		private void _zipTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidNumeralChar(e.KeyChar);
		}
	}
}
